<!-- 内容主体区域 -->
<link rel="stylesheet" href="layui/css/layui.css">
<style>
    .layui-table-cell .layui-form-checkbox[lay-skin="primary"]{
        top: 50%;
        transform: translateY(-50%);
    }
</style>
<div style="padding: 15px;">
    <div class="layui-tab">
        <div class="layui-tab-content">
            <table class="layui-table" id="test" lay-filter="test"></table>
            <button id="add" class="layui-btn layui-btn-sm" lay-event="add">添加球员</button>
        </div>
    </div>
</div>

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="layui/layui.js"></script>
<script>
    var url = location.search;
    var id;
    var Request = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            Request[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    }
    id = Request["id"];
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#test',
            url: 'http://svcsa.org/BBPlayer',
            parseData: function (res) {
                return {
                    "code": 0, //解析接口状态
                    "msg": '', //解析提示文本
                    "count": res.totalcount, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            method: 'get',
            request: {
                page: 'curr',
                pagesize: 'nums' //每页数据量的参数名，默认：limit
            },
            title: '用户数据表',
            cols: [
                [
                    {
                        type: 'checkbox',
                        fixed: 'left',
                        field: 'PlayerID',
                        width: 80,
//                    title: 'ID',
                    }, {
                    field: 'PlayerID',
                    title: 'ID',
                    width: 80,
                    fixed: 'left',
                    unresize: true,
                    sort: true,
                }, {
                    field: 'PlayerName',
                    title: '球员名称',
                    width: 150,
                }, {
                    field: 'Number',
                    title: '球员号码',
                    width: 100,
                }]
            ],
            page: false
        });
        var mycars = ''
        $("#add").click(function () {
            var data = table.checkStatus('test').data; //idTest 即为基础参数 id 对应的值
            mycars = '';
            if (data.length === 0) {
                layer.alert('请至少选择一名球员')
            } else {
                for (var i = 0; i < data.length; i++) {
                    if (i === data.length - 1) {
                        mycars += data[i].PlayerID
                    } else {
                        mycars += data[i].PlayerID + ','
                    }
                }
                console.log('mycars:' + mycars)
                let index = parent.layer.getFrameIndex(window.name);
                $.ajax({
                    url: 'http://svcsa.org/bbplayer/setteam',
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        PlayerIDs: HTMLDecode(mycars),
                        TeamID: id,
                    },
                    header: {
                        Accept: 'application/json'
                    },
                    success: function (data) {
                        console.log('添加成功执行--------')
                        console.log(data)
                        layer.msg('添加成功', {
                            icon: 1,
                            time: 1500
                        }, function () {
                            parent.layer.close(index);
                        });
                    },
                    error: function (data) {
                        console.log(data)
                    }
                });
            }

        })
    });

    function HTMLDecode(text) {
        var temp = document.createElement("div");
        temp.innerHTML = text;
        var output = temp.innerText || temp.textContent;
        temp = null;
        return output;
    }
</script>